import { Body, Controller, Get, Post, Res, UseInterceptors, Request, Query, HttpException, HttpStatus } from '@nestjs/common';
import { Response } from 'express';
import { RechargeFlowService } from './rechargeFlow.service';
import { apibefore } from 'src/router';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import utils from 'src/common/utils';
import { RedisCacheService } from '../redisCache/redisCache.service';
import { RateLimiterService } from '../rateLimiter/rateLimiter.service';


@Controller(apibefore('/rechargeFlow'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class RechargeFlowController {
  constructor(
    private readonly service: RechargeFlowService,
    private readonly redisCacheService: RedisCacheService, 
    private readonly rateLimiter: RateLimiterService, 
  ) {}


  /**
   * 查询用户充值记录
   * @param req 
   * @param params 
   * @returns 
   */
  @Get('/findMyRechargeFlow')
  async findMyRechargeFlow(@Request() req, @Query() params){
    let wx_user = req.headers.wx_user
    let pageIndex: number = Number(params.pageIndex || 0)
    let pageSize: number = Number(params.pageSize || 0)
    let cacheKey = `${wx_user}:consume_list:recharge:${pageIndex}_${pageSize}`
    let res = await this.redisCacheService.get(cacheKey)
    if(res){
      return res
    }
    let {skip, take} = utils.getLimit(pageSize, pageIndex)
    let data = await this.service.findMyRechargeFlow(skip, take, wx_user)
    await this.redisCacheService.set(cacheKey, data)
    return data
  }

  @Get('/findTotalAmountSuccessByWxUserStatisticsDay')
  async findTotalAmountSuccessByWxUserStatisticsDay(@Request() req, @Query() params){
    let wx_user = req.headers.wx_user
    let pageIndex: number = Number(params.pageIndex || 0)
    let pageSize: number = Number(params.pageSize || 0)
    let cacheKey = `${wx_user}:consume_total:recharge:${pageIndex}_${pageSize}`
    let res = await this.redisCacheService.get(cacheKey)
    if(res){
      return res
    }
    let {skip, take} = utils.getLimit(params.pageSize, params.pageIndex)
    let data = await this.service.findTotalAmountSuccessByWxUserStatisticsDay(skip, take, wx_user)
    await this.redisCacheService.set(cacheKey, data)
    return data
  }

}
